package com.yfproject.models;


import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.omg.CORBA.ServerRequest;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;



@Entity
@Table(name = "student")
public class Student {

    private Integer sid;
    private String studentName;
    private Set<Course> course;    //选修课程

    public Student() {

    }

    public Student(Integer sid, String studentName, Set<Course> course) {
        this.sid = sid;
        this.studentName = studentName;
        this.course = course;
    }

    @Id
    @Column(name = "s_id",unique = true, nullable = false, length = 32)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getSid() {
        return sid;
    }

    @Column(name = "student_name", unique = true, length = 64)
    public String getStudentName() {
        return studentName;
    }

    /**
     * Hibernate 会自动创建一张关系表stu_cou， 里边有俩字段stu_id和cou_id分别为两表主键
     *
     * @return
     */
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "cid") 
    @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
    @JoinTable(name = "stu_cou", joinColumns = {@JoinColumn(name = "stu_id")}, 
    	inverseJoinColumns = {@JoinColumn(name = "cou_id")})
    public Set<Course> getCourse() {
        return course;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public void setCourse(Set<Course> course) {
        this.course = course;
    }

}